﻿<!--
* Module Name:	TreeViewCrudDragDrop.xaml
* Project:		CSSLTreeViewCRUDDragDrop
* Copyright (c) Microsoft Corporation.
* 
* XAML representation of Custom Silverlight User Control which implements 
* a TreeView with added functionalities of CRUD and Drag-And-Drop
* 
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
* 
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
-->
    
<UserControl 
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  
    x:Class="CSSLTreeViewCRUDDragDrop.TreeViewCrudDragDrop"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="500" d:DesignWidth="500"
    xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
    xmlns:mswindows="clr-namespace:Microsoft.Windows;assembly=System.Windows.Controls.Toolkit"
    xmlns:my="clr-namespace:CSSLTreeViewCRUDDragDrop" >
    
    <UserControl.Resources>
        <!-- Template for Edit mode of TreeViewItem -->
        <sdk:HierarchicalDataTemplate x:Key="TreeViewMainEditTemplate" 
                                      ItemsSource="{Binding Children}">
            <TextBox Text="{Binding Text,Mode=TwoWay}" >
            </TextBox>
        </sdk:HierarchicalDataTemplate>
        <!-- Template for Read mode for TreeViewItem -->
        <sdk:HierarchicalDataTemplate x:Key="TreeViewMainReadTemplate" 
                                      ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Text,Mode=TwoWay}"               
                      MouseRightButtonDown="TreeViewMain_MouseRightButtonDown" 
                      MouseRightButtonUp="TreeViewMain_MouseRightButtonUp" 
                      MouseLeftButtonDown="TreeViewMain_MouseLeftButtonDown" >
            </TextBlock>
        </sdk:HierarchicalDataTemplate>
        <Style TargetType="toolkit:TreeViewDragDropTarget">
            <Setter Property="IsTabStop" Value="false" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="toolkit:TreeViewDragDropTarget">
                        <Grid>
                            <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Cursor="{TemplateBinding Cursor}" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                            <Canvas x:Name="InsertionIndicatorContainer" IsHitTestVisible="False">
                                <Path x:Name="InsertionIndicator" IsHitTestVisible="False" Stroke="Gray" StrokeThickness="2" />
                            </Canvas>
                            <Popup x:Name="DragPopup" IsHitTestVisible="False">
                                <Canvas x:Name="DragContainer" IsHitTestVisible="False">
                                    <my:MyDecorator x:Name="DragDecorator" IsHitTestVisible="False" Visibility="Collapsed" />
                                </Canvas>
                            </Popup>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">        
        <!-- TreeViewDragDropTarget from Toolkit to add DragAndDrop feature -->
        <toolkit:TreeViewDragDropTarget AllowDrop="True">
            <!-- Custom TreeView  -->
            <sdk:TreeView Name="TreeViewMain"    
                      ItemTemplate="{StaticResource TreeViewMainReadTemplate}"
                      MouseRightButtonDown="TreeViewMain_MouseRightButtonDown" 
                      MouseRightButtonUp="TreeViewMain_MouseRightButtonUp" 
                      MouseLeftButtonDown="TreeViewMain_MouseLeftButtonDown"                       
                      Width="500" Height="500"  >
            </sdk:TreeView>
        </toolkit:TreeViewDragDropTarget>
        
        <!-- Context Menu -->
        <Canvas>
            <Popup Name="ContextMenu" Visibility="Collapsed">
                <Border BorderThickness="1" BorderBrush="Black" Background="White">
                    <StackPanel>
                        <HyperlinkButton Content="Add" Name="AddButton" 
                                         Click="AddButton_Click" />
                        <HyperlinkButton Content="Edit" Name="EditButton" 
                                         Click="EditButton_Click"/>
                        <HyperlinkButton Content="Delete" Name="DeleteButton" 
                                         Click="DeleteButton_Click"/>
                    </StackPanel>
                </Border>
            </Popup>
        </Canvas>
    </Grid>
    
</UserControl>
